import pymysql

def sql_query(sql):
    conn=pymysql.connect(host="localhost",port=3306,user="root",password="root",db="lianxi",charset="utf8")
    cur=conn.cursor()
    num=cur.execute(sql)
    info=cur.fetchone()
    cur.close()
    conn.cursor()
    if num==1:
        return num,info[0]
    else:
        return num,None

def sql_modify(sql):
    conn=pymysql.connect(host="localhost",port=3306,user="root",password="root",db="lianxi",charset="utf8")
    cur=conn.cursor()
    num=cur.execute(sql)
    conn.commit()
    cur.close()
    conn.cursor()
    return num

def check_account(id):
    sql="SELECT id from atm where id={};".format(id)
    result=sql_query(sql)
    if result[0]==1:
        return True
    else:
        return False

def check_pwd(id,pwd):
    sql='SELECT id from atm where id={} and password={};'.format(id,pwd)
    result=sql_query(sql)
    if result[0]==1:
        return True
    else:
        return False

def check_money(id):
    sql="SELECT money from atm where id={}".format(id)
    result=sql_query(sql)
    return result[1]

def modify_money(id,money,sign):
    if sign=="+":
        sql="update atm set money=money+{} where id={}".format(money,id)
    else:
        sql="update atm set money=money-{} where id={}".format(money,id)
    if sql_modify(sql)==1:
        return True
    else:
        return False



mark=True
name=int(input("请输入正确的用户id: "))
if check_account(name)==True:
    times=1
    while mark:
        pwd=input("请输入正确的用户密码: ")
        if check_pwd(name,pwd)==True:
            print("密码输正确")
            while True:
                n = int(input("请按照提示输入以下数字完成对应的操作,1.查询账户 2.取款 3.转账 0.退出系统： "))
                if n == 1:
                    money=check_money(name)
                    print("你的存款余额有{}元".format(money))
                elif n==2:
                    money = check_money(name)
                    num=int(input("请输入你的取款金额: "))
                    if num>money:
                        print("你的余额不足")
                    else:
                        modify_money(name,num,"-")
                        money = check_money(name)
                        print("你的存款还有{}元".format(money))
                elif n==3:
                    name1=int(input("请输入你要转账的用户id"))
                    if check_account(name1)==False:
                        print("你的转账账户输入错误")
                    elif name==name1:
                        print("转入和转出的账户不能是同一个")
                    else:
                        money = check_money(name)
                        num = int(input("请输入你的转账金额: "))
                        if num > money:
                            print("你的余额不足")
                        else:
                            modify_money(name, num, "-")
                            modify_money(name1, num, "+")
                            money = check_money(name)
                            print("你的存款还有{}元".format(money))
                elif n==0:
                    print("退出系统")
                    mark=False
                    break
        else:
            if times>=3:
                break
            print("密码输入不正确")
            times+=1
else:
    print("用户id输入错误")




